package com.vcs.customtags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class RoundedCornerBoxTag extends TagSupport {

	Log log = LogFactory.getLog(RoundedCornerBoxTag.class);

	private static final long serialVersionUID = 1L;
	private PageContext pageContext;
	private Tag parentTag;
	// variables for tag attributes
	private int width;
	private String bgcolor = "";
	private String marginLeft = "";
	private String marginRight = "";
	private String fontColor = "";

	public void setWidth(int width) {
		this.width = width;
	}

	public void setBgcolor(String bgcolor) {
		this.bgcolor = bgcolor;
	}

	public void setFontColor(String fontColor) {
		this.fontColor = fontColor;
	}

	public void setMarginLeft(String marginLeft) {
		this.marginLeft = marginLeft;
	}

	public void setMarginRight(String marginRight) {
		this.marginRight = marginRight;
	}

	@Override
	public void setPageContext(PageContext pageContext) {
		this.pageContext = pageContext;
	}

	@Override
	public void setParent(Tag parentTag) {
		this.parentTag = parentTag;
	}

	@Override
	public Tag getParent() {
		return this.parentTag;
	}

	@Override
	public int doStartTag() throws JspException {
		String outerCss = "";
		String css = "";
		String wid = "";
		if (width == 0) {
			outerCss = outerCss + "width:100%;";
			wid = "100%";
		} else {
			outerCss = outerCss + "width:" + String.valueOf(width) + "px;";
			wid = String.valueOf(width);
		}

		// Margin-left-MarginRight
		if (marginLeft == null) {

		}

		if (marginRight == null) {

		} else if (marginRight.equalsIgnoreCase("auto")) {
			outerCss = outerCss + "margin-right:auto;";
		} else {
			outerCss = outerCss + "margin-right:" + marginRight + "px;";
		}

		if (marginLeft == null) {

		} else if (marginLeft.equals("auto")) {
			outerCss = outerCss + "margin-left:auto;";
		} else {
			outerCss = outerCss + "margin-left:" + marginLeft + "px;";
		}

		// Manage text color and background color

		if (bgcolor.equals("") || bgcolor == null) {
			css = css + "background-color:white;";
		} else {

			css = css + "background-color:" + bgcolor + ";";
		}

		if (fontColor == null) {

		} else if (fontColor.equals("")) {
			fontColor = "black";
			css = css + "color:black";
		} else {
			css = css + "color:" + fontColor + ";";
		}

		log.debug("OUTER CSS IS :" + outerCss);
		log.debug("INNER CSS IS :" + css);

		try {
			JspWriter out = pageContext.getOut();
			out.print("<div style=\"" + outerCss + "\"></b>");
			out.print("<b class=\"b2f\" style=\"" + css + "\"></b>");
			out.print("<b class=\"b3f\" style=\"" + css + "\"></b>");
			out.print("<b class=\"b4f\" style=\"" + css + "\"></b>");
			out.print("<div class=\"contentf\" style=\"" + css + "\">");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			JspWriter out = pageContext.getOut();
			out.print("</div><b class=\"b4f\" style=\"background-color:"
					+ bgcolor + "\"></b>");
			out.print("<b class=\"b3f\" style=\"background-color:" + bgcolor
					+ "\"></b>");
			out.print("<b class=\"b2f\" style=\"background-color:" + bgcolor
					+ "\"></b>");
			out.print("<b class=\"b1f\" style=\"background-color:" + bgcolor
					+ "\"></b></div>");

		} catch (Exception e) {
			e.printStackTrace();
		}

		return EVAL_PAGE;

	}
}
